<?php
/**
 * Yii view permit
 *
 * @author luubk
 * @since Jan 19, 2013 - 2:30:47 AM
 */
$auth = Yii::app()->authManager;
?>
<div class="grid">
    <div class="grid-title">
        <div class="pull-left"><?php echo $this->pageTitle ?></div>
        <div class="pull-right">            
        </div>
        <div class="clear"></div>
    </div>
    <table class="table table-striped table-mod-2">
        <thead>
            <tr>
                <th>#</th>
                <?php foreach ($auth->getRoles() as $role): ?>
                    <th style="text-align: center"><?php echo $role->name ?></th>
                <?php endforeach; ?>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($auth->getOperations() as $opera): ?>
                <tr>
                    <td><?php echo $opera->name ?></td>
                    <?php foreach ($auth->getRoles() as $role): ?>
                        <td style="text-align: center">
                            <a href="#" style="text-decoration: none"
                               onclick="return change_permit($(this), '<?php echo $role->name ?>', '<?php echo $opera->name ?>')">
                                <label style="cursor: pointer">
                                    <?php if ($role->hasChild($opera->name)): ?>
                                        <i class="icon icon-check"></i>
                                    <?php else: ?>
                                        <i class="icon icon-check-empty"></i>
                                    <?php endif; ?>                                    
                                </label>
                            </a>
                        </td>
                    <?php endforeach; ?>
                </tr>
            <?php endforeach; ?>                          
        </tbody>
    </table>
</div>
<script>
                           function change_permit(el, parent, child)
                           {
                               icon = el.children().children();
                               iconclass = icon.attr('class');

                               if (iconclass == 'icon icon-check')
                                   icon.attr('class', 'icon icon-check-empty');
                               else
                                   icon.attr('class', 'icon icon-check');

                               url = '<?php echo $this->createUrl('auth/permit', array('lang' => Yii::app()->language)) ?>';
                               $.post(url, {parent: parent, child: child}, function(data) {
                                   $.jGrowl(data.mess, {
                                       header: '<?php echo Yii::t('main', 'Notification') ?>',
                                       position: 'bottom-right'
                                   });
                               }, 'json');

                               return false;
                           }
</script>